<template>
  <div class="sqs">
    <el-button v-if="flag"
               @click="handleUpload">下载授权书</el-button>
    <el-button v-if="flag"
               @click="handleSend"
               type="primary"
               :loading="loading">发送授权书</el-button>
    <div class="content"
         v-if="flag">
      <avue-license ref="license"
                    :option="data"></avue-license>
    </div>
    <auth ref="auth"
          @submit="init">
    </auth>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      flag: false,
      form: {
        id: '',
        date: '',
        name: '',
        qq: ''
      }
    }
  },
  created () {

  },
  computed: {
    data () {
      return {
        img: "/images/sqstemp.jpg",
        list: [{
          left: 310,
          top: 625,
          text: `“ ${this.form.name} ”`,
          color: '#000',
          size: 29,
          bold: true,
          style: '黑体'
        }, {
          left: 690,
          top: 1077,
          text: this.form.id,
          color: '#000',
          size: 26,
          bold: true,
          style: '黑体'
        }, {
          left: 690,
          top: 1125,
          text: this.form.date,
          color: '#000',
          bold: true,
          size: 26,
          style: '黑体'
        }]
      }
    }
  },
  methods: {
    init () {
      if (this.$route.query.id) this.form.id = this.$route.query.id;
      if (this.$route.query.date) this.form.date = this.$route.query.date;
      if (this.$route.query.name) this.form.name = this.$route.query.name;
      if (this.$route.query.qq) this.form.qq = this.$route.query.qq + '@qq.com';
      this.flag = true;
    },
    handleSend () {
      this.loading = true;
      this.$refs.license.getFile(this.form.name).then(file => {
        const headers = { "Content-Type": "multipart/form-data" };
        let param = new FormData();
        param.append('file', file, file.name);
        param.append('qq', this.form.qq);
        param.append('id', this.form.id);
        param.append('token', 'avue');
        this.$httpajax.post('/upload', param, headers).then(() => {
          this.loading = false;
          this.$message.success('发送成功');
        }).catch(() => {
          this.$message.error('发送失败');
          this.loading = false;
        })
      });

    },
    handleUpload () {
      this.$refs.license.getPdf(this.form.name);
    }
  }
}
</script>

<style lang="scss">
.sqs {
  margin: 0 auto;
  width: 1000px;
  padding: 80px 0;
  .content {
    margin: 40px 0;
  }
}
</style>
